package com.open.exam.common;


import com.open.exam.examplan.*;

import java.util.List;

/**
 * @author 奶茶
 * 校验包
 * Created by on 2017/11/24.
 */
public class CheckPackage {

    private static CheckPackage instances;

    private ExamPlan examPlanObject;

    private String msg;

    static {
        if(instances == null){
            instances = new CheckPackage();
        }
    }

    public CheckPackage() {
    }

    public CheckPackage(ExamPlan examPlanObject) {
        this.examPlanObject = examPlanObject;
    }


    public static CheckPackage getInstances() {
        return instances;
    }

    public static void setInstances(CheckPackage instances) {
        CheckPackage.instances = instances;
    }

    public ExamPlan getExamPlanObject() {
        return examPlanObject;
    }

    public void setExamPlanObject(ExamPlan examPlanObject) {
        this.examPlanObject = examPlanObject;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }


    /**
     * 验证考生数目必须大于0
     * @param examPlan
     * @return
     */
    public Boolean validateExamExamineeNum(ExamPlan examPlan){
        List<Examiner> examinees = examPlan.getExamSite().getExaminers();
        if(examinees.size() <= 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 验证同一考生不能在同一场次的不同考场出现
     * @param examPlan
     * @return
     */
    public Boolean validateExamineeRepeatInRoom(ExamPlan examPlan){
        //同一场次不同考场
        //得到考试编排数组
        List<ExamArrange> examArranges = examPlan.getExamSite().getExamArranges();
                //取得考场数组
                for (int i = 0; i< examArranges.size(); i++){
                    for(int j = i+1; j< examArranges.size(); j++){
                        if(examArranges.get(i).getSceneID().equals(examArranges.get(j).getSceneID())){
                            List<ExamRoomArrange> examineeArrange = examArranges.get(i).getExamRoomArranges();
                            List<ExamRoomArrange> examineeArrange1 = examArranges.get(j).getExamRoomArranges();
                            for(int k=0;k<examineeArrange.size();k++){
                                for (int r=0;r<examineeArrange1.size();r++){
                                    if(examineeArrange.get(k).getExaminerID().equals(examineeArrange1.get(r).getExaminerID())){
                                        return false;
                                    }
                                }
                            }
                        }
                    }
                }
        return true;
    }

    /**
     * 验证同一考生不能在同一场次下不同科目出现
     * @param examPlan
     * @return
     */
    public Boolean validateExamineeRepeatInSubject(ExamPlan examPlan){
        List<ExamArrange> examArranges = examPlan.getExamSite().getExamArranges();
        for (int i = 0; i< examArranges.size(); i++){
            for(int j = i+1; j< examArranges.size(); j++){
                if((examArranges.get(i)).getSceneID().equals((examArranges.get(j)).getSceneID())){
                    List<ExamRoomArrange> examRoomArranges = examArranges.get(i).getExamRoomArranges();
                    List<ExamRoomArrange> examRoomArranges1 = examArranges.get(j).getExamRoomArranges();
                    ExamRoomArrange examRoomArrangeFirst = examRoomArranges.stream().map(
                            examRoomArrange -> examRoomArranges1.stream()
                                    .filter(examRoomArrange1 -> examRoomArrange1.getExaminerID().equals(examRoomArrange.getExaminerID()))

                                    .findFirst()
                                    .orElse(new ExamRoomArrange("0",0))
                    ).findFirst().orElse(null);
                    if(examRoomArrangeFirst != null && !("0").equals(examRoomArrangeFirst.getExaminerID())){
                        this.msg = "考生ID为"+ examRoomArrangeFirst.getExaminerID()+"在场次ID为"+ examArranges.get(i).getSceneID()+"中重复出现";
                        return false;
                    }
                }
            }
        }
        return true;
    }

    /**
     * 验证考场数目必须大于0
     * @param examPlan
     * @return
     */
    public Boolean validateExamRoomsNum(ExamPlan examPlan){
        List<ExamRoom> examRooms = examPlan.getExamSite().getExamRooms();
        if(examRooms.size() <= 0){
            return false;
        }else{
            return true;
        }
    }

    /**
     * 按场次开始时间对场次进行排序
     * @param examPlan
     * @return
     */
    public List<ExamScene> getExamScenesArrayList(ExamPlan examPlan){
        List<ExamScene> examScenes = examPlan.getExamSite().getExamScenes();
        return examScenes;
    }

    /**
     * 得到最大科目考试时间
     * @param examPlan
     * @param examScenesID
     * @return
     */
    private int getMaxExamSubjectTime(ExamPlan examPlan,String examScenesID){
        List<ExamSubject> examSubjects = examPlan.getExamSite().getExamSubjects();

        int iMaxTime = 0;
        for (ExamSubject object:examSubjects) {
                for (ExamArrange o :examPlan.getExamSite().getExamArranges()) {
                    if(o.getSceneID().equals(examScenesID) && o.getSubjectID().equals(object.getSubjectID())){
                        if(iMaxTime < object.getTimeLimits()){
                            iMaxTime = object.getTimeLimits();
                        }
                    }
                }
        }
        return iMaxTime;
    }
}
